<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单编号" prop="orderId">
        <el-select v-model="queryParams.orderId" placeholder="请选择订单编号" clearable size="small">
          <el-option
            v-for="ID in order_ID"
            :key="ID.orderId"
            :value="ID.orderId"
            :label="ID.orderId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerId">
        <el-select v-model="queryParams.customerId" placeholder="请选择客户名称" clearable size="small">
          <el-option
            v-for="Id in customer_ID"
            :key="Id.customerId"
            :value="Id.customerId"
            :label="Id.contactPerson"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提苗人姓名" prop="takeseedlingPerson">
        <el-input
          v-model="queryParams.takeseedlingPerson"
          placeholder="请输入提苗人姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提苗人电话" prop="takeseedlingPhone">
        <el-input
          v-model="queryParams.takeseedlingPhone"
          placeholder="请输入提苗人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大棚名称" prop="greenhouseId">
        <el-select v-model="queryParams.greenhouseId" placeholder="请选择大棚名称" clearable size="small">
          <el-option
            v-for="ID in greenhouse_ID"
            :key="ID.greenhouseId"
            :value="ID.greenhouseId"
            :label="ID.greenhouseName"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出圃时间" prop="outgreenhouseTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.outgreenhouseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出圃时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['transplant:transplant:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['transplant:transplant:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['transplant:transplant:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['transplant:transplant:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="transplantList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="订单编号" align="center" prop="orderId" />
      <el-table-column label="订单名称" align="center" prop="orderName" />
      <el-table-column label="客户编号" align="center" prop="customerId" />
      <el-table-column label="客户名称" align="center" prop="contactPerson" />
      <el-table-column label="提苗人姓名" align="center" prop="takeseedlingPerson"width="130" />
      <el-table-column label="提苗人电话" align="center" prop="takeseedlingPhone"width="130" />
      <el-table-column label="大棚名称" align="center" prop="greenhouseName" />
      <el-table-column label="出圃时间" align="center" prop="outgreenhouseTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outgreenhouseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="数量" align="center" prop="transplantAmount" />
      <el-table-column label="总价" align="center" prop="totalPrice" />
      <el-table-column label="备注" align="center" prop="note" />
      <el-table-column label="片区经理审核状态" align="center" prop="approvalStatusManager" width="130">
        <template slot-scope="scope"><span>
          {{approvalStatus[scope.row.approvalStatusManager]}}
        </span>
          <div style="display:flex">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handlethroughpianqu(scope.row)"
              v-hasPermi="['transplant:transplant:edit']"
            >通过</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleNotThroughpianqu(scope.row)"
              v-hasPermi="['transplant:transplant:edit']"
            >不通过</el-button>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="销售经理审核状态" align="center" prop="approvalStatusSale"  width="130">
        <template slot-scope="scope"><span>
          {{approvalStatus[scope.row.approvalStatusSale]}}</span>
          <div style="display:flex">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handlethroughxiaoshou(scope.row)"
              v-hasPermi="['transplant:transplant:edit']"
            >通过</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleNotThroughxiaoshou(scope.row)"
              v-hasPermi="['transplant:transplant:edit']"
            >不通过</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"width="100">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['transplant:transplant:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['transplant:transplant:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改出圃管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="订单名称" prop="orderId">
          <el-select v-model="form.orderId" placeholder="请选择订单名称" clearable size="small">
            <el-option
              v-for="ID in order_ID"
              :key="ID.orderId"
              :value="ID.orderId"
              :label="ID.orderName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名称" prop="customerId">
          <el-select v-model="form.customerId" placeholder="请选择客户名称" clearable size="small">
            <el-option
              v-for="Id in customer_ID"
              :key="Id.customerId"
              :value="Id.customerId"
              :label="Id.contactPerson"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="提苗人姓名" prop="takeseedlingPerson">
          <el-input v-model="form.takeseedlingPerson" placeholder="请输入提苗人姓名" />
        </el-form-item>
        <el-form-item label="提苗人电话" prop="takeseedlingPhone">
          <el-input v-model="form.takeseedlingPhone" placeholder="请输入提苗人电话" />
        </el-form-item>
        <el-form-item label="大棚名称" prop="greenhouseId">
          <el-select v-model="form.greenhouseId" placeholder="请选择大棚名称" clearable size="small">
            <el-option
              v-for="ID in greenhouse_ID"
              :key="ID.greenhouseId"
              :value="ID.greenhouseId"
              :label="ID.greenhouseName"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出圃时间" prop="outgreenhouseTime">
          <el-date-picker clearable size="small"
            v-model="form.outgreenhouseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出圃时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="数量" prop="transplantAmount">
          <el-input v-model="form.transplantAmount" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="总价" prop="totalPrice">
          <!--<el-input v-model="form.totalPrice" placeholder="请输入总价" />-->
          {{getprice}}
        </el-form-item>
        <el-form-item label="备注" prop="note">
          <el-input v-model="form.note" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTransplant, getTransplant, delTransplant, addTransplant, updateTransplant, exportTransplant,listCustomer,listOrder,listgreenhouse } from "@/api/transplant/transplant";

export default {
  name: "Transplant",
  components: {
  },
  data() {
    return {
      //审核状态
      approvalStatus:{
        0:'未审核',
        1:'审核通过',
        2:'不通过'
      },
      // 遮罩层
      customer_ID:[],
      order_ID:[],
      greenhouse_ID:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出圃管理表格数据
      transplantList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        customerId: null,
        takeseedlingPerson: null,
        takeseedlingPhone: null,
        greenhouseId: null,
        outgreenhouseTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单编号不能为空", trigger: "change" }
        ],
        customerId: [
          { required: true, message: "客户编号不能为空", trigger: "change" }
        ],
        takeseedlingPerson: [
          { required: true, message: "提苗人姓名不能为空", trigger: "blur" }
        ],
        takeseedlingPhone: [
          { required: true, message: "提苗人电话不能为空", trigger: "blur" }
        ],
        greenhouseId: [
          { required: true, message: "大棚编号不能为空", trigger: "change" }
        ],
        outgreenhouseTime: [
          { required: true, message: "出圃时间不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "单价不能为空", trigger: "blur" }
        ],
        transplantAmount: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        approvalStatusManager: [
          { required: true, message: "片区主管审批状态不能为空", trigger: "blur" }
        ],
        approvalStatusSale: [
          { required: true, message: "销售经理审批状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getcustomerID();
    this.getorderID();
    this.getgreenhouseID();
  },
  computed:{getprice(){return this.form.transplantAmount*this.form.price}},
  methods: {
    /**片区通过按钮操作*/
    handlethroughpianqu(row){
      this.reset();
      const transplantId = row.transplantId||this.ids
      let data={}
      data.transplantId=transplantId;
      data.approvalStatusManager=1;
      this.$confirm('是否确认通过序号为"'+transplantId+'"的数据项？',"提示",{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(function(){
        return updateTransplant(data).then(response=>{
          console.log(response)
        })
      }).then(()=>{
        this.getList();
      }).catch(()=>{});
    },
    /**片区不通过按钮操作*/
    handleNotThroughpianqu(row){
      const transplantId = row.transplantId||this.ids;
      let data={}
      data.transplantId=transplantId;
      data.approvalStatusManager=2;
      this.$confirm('是否确认不通过序号为"'+transplantId+'"的数据项？',"警告",{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(function(){
        return updateTransplant(data).then(response=>{
          console.log(response)
        })
      }).then(()=>{
        this.getList();
      }).catch(()=>{});
    },
    /**销售通过按钮操作*/
    handlethroughxiaoshou(row){
      this.reset();
      const transplantId = row.transplantId||this.ids
      let data={}
      data.transplantId=transplantId;
      data.approvalStatusSale=1;
      this.$confirm('是否确认通过序号为"'+transplantId+'"的数据项？',"提示",{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(function(){
        return updateTransplant(data).then(response=>{
          console.log(response)
        })
      }).then(()=>{
        this.getList();
      }).catch(()=>{});
    },
    /**销售不通过按钮操作*/
    handleNotThroughxiaoshou(row){
      const transplantId = row.transplantId||this.ids;
      let data={}
      data.transplantId=transplantId;
      data.approvalStatusSale=2;
      this.$confirm('是否确认不通过序号为"'+transplantId+'"的数据项？',"警告",{
        confirmButtonText:"确定",
        cancelButtonText:"取消",
        type:"warning"
      }).then(function(){
        return updateTransplant(data).then(response=>{
          console.log(response)
        })
      }).then(()=>{
        this.getList();
      }).catch(()=>{});
    },
    /**获取订单名称*/
    formdata_sepaseedlingOrderList(data){
      let sepaseedling_type_map = new Map();
      this.order_ID.forEach(type =>{
        sepaseedling_type_map.set(type.orderId,type.orderName)
      })
      data.forEach(type => {
        let order_name = sepaseedling_type_map.get(type.orderId)
        console.log(order_name)
        type.orderName = order_name
        console.log(type.orderName)
      });
    },
    /**获取客户名称*/
    formdata_sepaseedlingUserList(data){
      let sepaseedling_type_map = new Map();
      this.customer_ID.forEach(type =>{
        sepaseedling_type_map.set(type.customerId,type.contactPerson)
      })
      data.forEach(type => {
        let user_contactPerson = sepaseedling_type_map.get(type.customerId)
        type.contactPerson = user_contactPerson
        console.log(type.contactPerson)
      });
    },
    /**获取大棚名称*/
    formdata_sepaseedlingGreenhouseList(data){
      let sepaseedling_type_map = new Map();
      this.greenhouse_ID.forEach(type =>{
        sepaseedling_type_map.set(type.greenhouseId,type.greenhouseName)
      })
      data.forEach(type => {
        let user_greenhouseName = sepaseedling_type_map.get(type.greenhouseId)
        type.greenhouseName = user_greenhouseName
        console.log(type.greenhouseName)
      });
    },
    /** 查询出圃管理列表 */
    getList() {
      this.loading = true;
      listTransplant(this.queryParams).then(response => {
        this.transplantList = response.rows;
        this.total = response.total;
        this.loading = false;
        this.formdata_sepaseedlingOrderList(this.transplantList);
        this.formdata_sepaseedlingUserList(this.transplantList);
        this.formdata_sepaseedlingGreenhouseList(this.transplantList)
      });
    },
    getcustomerID(){
      listCustomer().then(response =>{
        console.log(response)
        this.customer_ID = response.rows;
      })
    },
    getorderID(){
      listOrder().then(response =>{
        console.log(response)
        this.order_ID = response.rows;
      })
    },
    getgreenhouseID(){
      listgreenhouse().then(response =>{
        console.log(response)
        this.greenhouse_ID = response.rows;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        transplantId: null,
        orderId: null,
        customerId: null,
        takeseedlingPerson: null,
        takeseedlingPhone: null,
        greenhouseId: null,
        outgreenhouseTime: null,
        price: null,
        transplantAmount: null,
        totalPrice: null,
        note: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        approvalStatusManager: null,
        approvalStatusSale: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.transplantId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加出圃管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const transplantId = row.transplantId || this.ids
      getTransplant(transplantId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出圃管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.transplantId != null) {
            this.form.totalPrice=this.getprice
            updateTransplant(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.totalPrice=this.getprice
            addTransplant(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const transplantIds = row.transplantId || this.ids;
      this.$confirm('是否确认删除出圃管理编号为"' + transplantIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTransplant(transplantIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有出圃管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTransplant(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
